/**
 * 
 */
package com.smarthome.api.mobile.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.smarthome.common.mobile.Service;
import com.smarthome.common.tools.APIUtil;
import com.smarthome.core.advicemanager.PlatformAdviceService;
import com.smarthome.core.advicemanager.PropertyAdviceService;
import com.smarthome.domain.bean.ShPlatformNotice;
import com.smarthome.domain.bean.ShPropertyNotice;
import com.smarthome.domain.mobile.MobileCodeEnum;
import com.smarthome.domain.mobile.MobileResponse;
import com.smarthome.domain.mobile.req.MobileNoticeRequest;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * @author dhc
 * @date 2015年10月16日 下午4:48:31
 * @title 公告获取
 */
@org.springframework.stereotype.Service("mobile.notice")
public class MobileNoticeService implements Service<MobileResponse<?>> {

	@Resource(name="platformAdviceService")
	private PlatformAdviceService platformAdviceService;
	
	@Resource(name="propertyAdviceService")
	private PropertyAdviceService propertyAdviceService;
	
	
	/**
	 * @title  获取最顶端公告
	 * @createTime 2015年10月16日 下午4:58:09
	 * @author dhc
	 */
	public void topplatform (MobileNoticeRequest request, MobileResponse<Object> response){
		PageHelper page = new PageHelper();
		page.setPage(1);
		page.setRows(1);
		page.setOrder("DESC");
		page.setSort("publishTime");
		ShPlatformNotice platformNotice = new ShPlatformNotice();
		platformNotice.setStatus(1);
		DataGrid dataGrid = platformAdviceService.qeuryDataGrid(platformNotice, page);
		List <Object> list = dataGrid.getRows();
		if(null != list && list.size() > 0){
			response.setData(list.get(0));
		} else {
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_FALSE);
		}
		
	}
	
	/**
	 * @title  平台公告单条详情
	 * @createTime 2015年10月16日 下午6:28:42
	 * @author dhc
	 */
	public void platformDetail(MobileNoticeRequest request, MobileResponse<Object> response){
		if(null == request.getId()){
			response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_NOT_FULL);
			return ;
		}
		response.setData(platformAdviceService.queryShPlatformNoticeById(request.getId()));
		
	}
	
	/**
	 * @title （社区）平台公告列表
	 * @createTime 2015年10月16日 下午5:08:24
	 * @author dhc
	 */
	public void platform(MobileNoticeRequest request, MobileResponse<Object> response){
		PageHelper page = new PageHelper();
		request.getPageSize();
		page.setPage(request.getPage());
		page.setRows(request.getPageSize());
		page.setOrder("DESC");
		page.setSort("publishTime");
		ShPlatformNotice platformNotice = new ShPlatformNotice();
		platformNotice.setStatus(1);  
		DataGrid dataGrid = platformAdviceService.qeuryDataGrid(platformNotice, page);
		List <Object> list = dataGrid.getRows();
		if(null != list && list.size() > 0){
			response.setData(list);
		} else {
		//	response.setMobileCodeObj(MobileCodeEnum.PAGE_NOTSHOW);
			response.setData(new ArrayList<>());
		}
		
	}
	
	/**
	 * @title 物业公告
	 * @createTime 2015年10月16日 下午5:08:55
	 * @author dhc
	 */
	public void property(MobileNoticeRequest request, MobileResponse<Object> response){
		if(!APIUtil.isLogin(request)){
			response.setMobileCodeObj(MobileCodeEnum.NOT_LOGIN_OPTION);
			return ;
		}
		Long residenceId = request.getAccount().getResidenceId();
		if(null == residenceId){
			response.setMobileCodeObj(MobileCodeEnum.VISIT_RESTRICT);
			return ;
		}
		
		PageHelper page = new PageHelper();
		request.getPageSize();
		page.setPage(request.getPage());
		page.setRows(request.getPageSize());
		page.setOrder("DESC");
		page.setSort("publishTime");
		new ShPropertyNotice();
		ShPropertyNotice notice =new ShPropertyNotice();
		notice.setResidenceId(residenceId);
		notice.setStatus(1);
		DataGrid dataGrid = propertyAdviceService.qeuryDataGrid(notice, page);
		List <Object> list = dataGrid.getRows();
		if(null != list && list.size() > 0){
			response.setData(list);
		} else {
			response.setData(new ArrayList<>());
			//response.setMobileCodeObj(MobileCodeEnum.PAGE_NOTSHOW);
		}
	}
	
	/**
	 * @title 物业详情页面
	 * @createTime 2015年10月16日 下午6:28:42
	 * @author dhc
	 */
	public void propertyDetail(MobileNoticeRequest request, MobileResponse<Object> response){
		if(null == request.getId()){
			response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_NOT_FULL);
			return ;
		}
		ShPropertyNotice notice = propertyAdviceService.queryShPropertyNoticeById(request.getId());
		if(null == notice){
			notice = new ShPropertyNotice();
		}
		response.setData(notice);
		
	}
}
